/*
 * Copyright (c) 2022 Proton Technologies AG
 * This file is part of Proton Technologies AG and Proton Mail.
 *
 * Proton Mail is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Proton Mail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Proton Mail. If not, see <https://www.gnu.org/licenses/>.
 */

package ch.protonmail.android.uitest.robot.composer.section

import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import ch.protonmail.android.mailcomposer.presentation.ui.AddAttachmentsBottomSheetTestTags
import ch.protonmail.android.test.ksp.annotations.AttachTo
import ch.protonmail.android.test.ksp.annotations.VerifiesOuter
import ch.protonmail.android.uitest.robot.ComposeSectionRobot
import ch.protonmail.android.uitest.robot.composer.ComposerRobot
import ch.protonmail.android.uitest.util.awaitDisplayed
import ch.protonmail.android.uitest.util.awaitHidden
import ch.protonmail.android.uitest.util.child

@AttachTo(targets = [ComposerRobot::class], identifier = "attachmentsBottomSheet")
internal class ComposerAttachmentsBottomSheetSection : ComposeSectionRobot() {

    private val rootItem = composeTestRule.onNodeWithTag(
        AddAttachmentsBottomSheetTestTags.RootItem, useUnmergedTree = true
    )

    private val importRootItem = rootItem.child {
        hasTestTag(AddAttachmentsBottomSheetTestTags.ImportEntry)
    }

    private val importIcon = importRootItem.child {
        hasTestTag(AddAttachmentsBottomSheetTestTags.ImportIcon)
    }

    private val importText = importRootItem.child {
        hasTestTag(AddAttachmentsBottomSheetTestTags.ImportText)
    }

    fun tapImportItem() = withVisibleItem {
        importText.performClick()
    }

    @VerifiesOuter
    inner class Verify {

        fun isShown() {
            rootItem.awaitDisplayed()
        }

        // No need for specific entry item models now.
        fun hasImportOption() = withVisibleItem {
            importIcon.awaitDisplayed()
            importText.awaitDisplayed()
        }

        fun isDismissed() {
            rootItem.awaitHidden()
        }
    }

    private fun withVisibleItem(block: ComposerAttachmentsBottomSheetSection.() -> Unit) {
        rootItem.awaitDisplayed()
        block()
    }
}
